package com.edward.action;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.DrawBack;
import com.edward.service.DrawBackManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DrawBackAction extends ActionSupport{
	

	private static final long serialVersionUID = 3091356021085207975L;
	
	private DrawBack drawBack;
	
	private int start;
	
	private int limit;
	
	private DrawBackManager drawBackManager;
	

	public DrawBack getDrawBack() {
		return drawBack;
	}

	public void setDrawBack(DrawBack drawBack) {
		this.drawBack = drawBack;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setDrawBackManager(DrawBackManager drawBackManager) {
		this.drawBackManager = drawBackManager;
	}
	
	@Secured("ROLE_FINANCE")
	public String  add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		drawBack.setSum(drawBackManager.getSum() + drawBack.getTax());
		drawBackManager.add(drawBack);
		int total = drawBackManager.listDrawBack().size();
		response.getWriter().print("{success : true , msg : '添加成功!',total:" + total + "}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		DrawBack d = drawBackManager.getDrawBack(drawBack.getId());
		drawBack.setSum(d.getSum());
		drawBackManager.update(drawBack);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		return null;
		
	}
	
	@Secured("ROLE_FINANCE")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		pager = drawBackManager.pagerList(start, limit);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
}
